﻿Imports System.IO

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim url As String = "http://www.forosdelweb.com/images/vbulletin3_logo_fdw.gif"
        Dim url2 As String = "http://www.forosdelweb.com/images/vbulletin3_logo_fdw.gif"

        Dim im1 As Image = imageUrlToImage(url)
        Dim im2 As Image = imageUrlToImage(url)

        Me.PictureBox1.Image = Me.MergeImages(im1, im2)
    End Sub

    Private Function imageUrlToImage(ByVal url As String) As Image

        Dim miRequest As System.Net.WebRequest = System.Net.WebRequest.Create(url)
        Dim miResponse As System.Net.WebResponse = miRequest.GetResponse


        Return (Image.FromStream(miResponse.GetResponseStream()))

    End Function

    Public Shared Function Bytes2Image(ByVal bytes() As Byte) As Image
        If bytes Is Nothing Then Return Nothing
        '
        Dim ms As New MemoryStream(bytes)
        Dim bm As Bitmap = Nothing
        Try
            bm = New Bitmap(ms)
        Catch ex As Exception
            System.Diagnostics.Debug.WriteLine(ex.Message)
        End Try
        Return bm
    End Function

    Public Function MergeImages(ByVal Pic1 As Image, ByVal pic2 As Image) As Image


        Dim MergedImage As Image ' This will be the finished merged image

        Dim Wide, High As Integer ' Size of merged image
        ' Calculate Width and Height needed for composite image
        ' First, the Width:
        High = Pic1.Height + pic2.Height

        ' Height: Ensure that the new image is high enough for both images
        ' that we plan to place inside it.
        If Pic1.Width >= pic2.Width Then
            Wide = Pic1.Width
        Else
            Wide = pic2.Width
        End If

        ' Create an empty Bitmap the correct size to hold both images side by side
        Dim bm As New Bitmap(Wide, High)
        ' Get the Graphics object for this bitmap
        Dim gr As Graphics = Graphics.FromImage(bm)

        ' Draw a black line round the outside (optional, but sometimes looks better when printed)
        gr.DrawRectangle(Pens.Black, 0, 0, Wide - 1, High - 1)
        ' Draw the first source image at left side of new image
        gr.DrawImage(Pic1, 0, 0)
        ' Draw second source image, offset to the right edge of first source image
        gr.DrawImage(pic2, 0, Pic1.Height)

        ' Assign the merged bitmap you have just created as the image
        ' you are going to return for printing
        MergedImage = bm

        ' Finished with the Graphics object – dispose of it
        gr.Dispose()

        ' You now have an Image named MergedImage which you can print.
        Return MergedImage

    End Function

End Class
